package cupiPhoneInterfaz;

import java.awt.GridBagLayout;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JList;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import cupiPhoneMundo.Contacto;
import javax.swing.ListSelectionModel;



public class PanelContactos extends JPanel
{

    private static final long serialVersionUID = 1L;
    private JList jListContacos = null;
    private JLabel back = null;
    private JLabel jLabelcont = null;
    private CupiPhoneInterfaz principal;
    private ArrayList contactos;
    private JList listaRegistroContactos = null;


    /**
     * This is the default constructor
     */
    public PanelContactos(CupiPhoneInterfaz cpi )
    {
        super( );
        initialize( );
        principal = cpi;
        contactos = new ArrayList();
        
        this.actualizar();
    }
  

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize( )
    {
        jLabelcont = new JLabel();
        jLabelcont.setBounds(new Rectangle(6, 3, 138, 15));
        jLabelcont.setForeground(new Color(204, 204, 204));
        jLabelcont.setText("Contactos");
        back = new JLabel();
        back.setText("");
        back.setSize(new Dimension(320, 480));
        back.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
        back.setIcon(new ImageIcon("C:/tempo2/cupiPhone/data/backgrund.gif"));
        back.setLocation(new Point(0, -4));
        this.setSize(300, 173);
        this.setLayout(null);
        this.setBorder(BorderFactory.createTitledBorder(null, "Contactos", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(204, 204, 204)));
        this.add(getJListContacos(), null);
        this.add(jLabelcont, null);
        this.add(back, null);
        this.add(getListaRegistroCanciones(), null);
    }
    
    private JList getListaRegistroCanciones() {
        if (listaRegistroContactos == null) {
            listaRegistroContactos = new JList();
            listaRegistroContactos.setLocation(new Point(5, 21));
            listaRegistroContactos.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            listaRegistroContactos.setSize(new Dimension(290, 146));
        }
        return listaRegistroContactos;
    }


    /**
     * This method initializes jListContacos	
     * 	
     * @return javax.swing.JList	
     */
    private JList getJListContacos( )
    {
        if( jListContacos == null )
        {
            jListContacos = new JList( );
            jListContacos.setBounds(new Rectangle(5, 21, 290, 146));
            
        }
        return jListContacos;
    }
    
    private void darContactos(ArrayList contactos)
    {
        principal.darContactos( contactos );
    }
    
    public void actualizar() 
    {
        
        listaRegistroContactos.removeAll();              
        contactos = new ArrayList( );
        darContactos( contactos );
      
        if (contactos != null)
        {
        
        DefaultListModel modelo = new DefaultListModel();
        for (int i = 0; i < contactos.size( ); i++)
        {
            
            String c = (String)contactos.get( i );
            modelo.addElement( c );
        }
        listaRegistroContactos.setModel(modelo);
        }
//        IIterador<ICancion> i = principal.darCanciones();
//        DefaultListModel modelo = new DefaultListModel();
//        int can = 0;
//        while (i.haySiguiente()) {
//            ICancion c = i.darSiguiente();
//            if( c != null )
//            {
//            modelo.addElement(c);
//            can++;
//            }
//        }
//        listaRegistroCanciones.setModel(modelo);
//        this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.yellow, 5), "Registro Canciones: " +can, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.white));
        
        
        // TODO Auto-generated method stub
    }
    
    

}  //  @jve:decl-index=0:visual-constraint="10,10"
